﻿@model MvcExamples.Models.Order
@{
    ViewBag.Title = "BÁN HÀNG";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        // Document.ready -> link up remove event handler
        $(".CustomerId").change(function () {
            // Get the id from the link

            var v = document.getElementById('CustomerId').value
            var txtName = document.getElementById("Name");
            var txtAddress = document.getElementById("Address");
            var txtCustomerType = document.getElementById("CustomerType");
            txtCustomerType.innerHTML 
            if (v != null && v != '') {

                $.post("/Customer/QuickSearch", { "id": v },
                        function (data) {
                            // get the result and do some magic with it

                            txtName.value = data.Name;
                            txtName.setAttribute('readonly', true);
                            txtAddress.value = data.Address;
                            txtAddress.setAttribute('readonly', true);
                            txtCustomerType.innerHTML = data.CustomerTypeName;

                        });

            } else {
                txtName.removeAttribute('readOnly');
                txtName.value = "";
                txtAddress.removeAttribute('readOnly');
                txtAddress.value = ""; 
                txtCustomerType.innerHTML = "Vãng Lai"
            }
            
        });
    });

</script>
@if (Model == null)
{

    using (Html.BeginForm())
    {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Thông Tin Khách Hàng</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.CustomerId)
        </div>
        <div class="CustomerId">
            @Html.DropDownList("CustomerId", String.Empty)
            @Html.ValidationMessageFor(model => model.CustomerId)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)
        </div>
        <div class="editor-label">
            Loại Khách Hàng
        </div>

        <div class="editor-field">
            <label id="CustomerType"> Vãng Lai</label>      
        </div>
        
         <input type="submit" value="Tạo Thông Tin" />        
    </fieldset>
   
    }

}
else
{   
    
 using (Html.BeginForm("Create","OrderDetail"))
    {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>THÔNG TIN KHÁCH HÀNG</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.Address)
        </div>
         <div class="editor-label">
            Loại Khách Hàng
        </div>
        <div class="display-field">
            xxxxx
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Date)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.Date)
        </div>
    </fieldset>
    
       if (Model.OrderDetails != null)
       {          
           <fieldset>
           <legend> Chi Tiết Đơn Hàng</legend>
           @{Html.RenderPartial("../Order/_OrderDetailPartial", Model.OrderDetails);}

           </fieldset>
           @*Html.RenderAction("index", "OrderDetail", new { id = Model.OrderId });*@
        } 
       
    <table >
        <tr>
            <th>
                Hàng Hóa
            </th>
            <th>
                Đơn Vị
            </th>
            <th>
                Số Lượng
            </th>
            <th>
            </th>
        </tr>
        <tr>
            <th>
                @Html.DropDownList("ProductId", String.Empty)
            </th>
            <th>
                @Html.DropDownList("ProductUnitId", String.Empty)
            </th>
            <th>
                @Html.Editor("Amount")
            </th>
            <th>
                <input type="submit" value="Thêm" />              
            </th>
        </tr>
        
    </table>
    <div class="error">
            @ViewBag.Message
        </div>
    }
}
